<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
    integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>

<body>
  <div class="container">
    <nav class="navbar navbar-default navbar-inverse">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
            data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">全能网后台管理系统</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li><a href="./index.html">用户管理<span class="sr-only">(current)</span></a></li>
            <li><a href="./product.html">商品管理</a></li>
            <li><a href="#">地址管理</a></li>
            <li class="active"><a href="./category.html">分类管理</a></li>
            <li><a href="#">售后管理</a></li>
          </ul>
          <form class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search" id="searchText">
            </div>
            <button type="submit" class="btn btn-default" id="searchBtn">搜索</button>
          </form>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Link</a></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    <!-- 上传分类 -->
    <div class="wrap">
      输入一级导航： <input type="text" class="form-control" id="oneText" aria-describedby="helpBlock2">
      <button class="btn btn-success" id="oneBtn">上传一级分类</button>
    </div>
    <div>
      <h1>选择要添加二级分类的选项：</h1>
      <select name="" id="mysel" class="form-control">

      </select>
      <input type="text" class="form-control" id="twoText" aria-describedby="helpBlock2">
      <button class="btn btn-success" id="twoBtn">上传二级分类</button>
    </div>
  </div>

  <script src="./javascripts/jquery-3.4.1.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
    integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
    crossorigin="anonymous"></script>
  <script>
    // 添加一级分类
    $("#oneBtn").click(function () {
      $.ajax({
        url: "http://127.0.0.1:3000/cate/addOneCate",
        type: "post",
        data: {
          cn_name: $("#oneText").val()
        },
        success: function (res) {
          console.log(res)
          if (res.err) {
            alert(res.err)
          }
          if (res.success) {
            alert("添加一级分类成功")
            showOne()
          }
        }
      })
    })
    // 获取一级分类并渲染
    function showOne() {
      $.ajax({
        url: "http://127.0.0.1:3000/cate/all",
        success: function (res) {
          console.log(res)
          var data = res.reverse();
          var str = ``
          $.each(data, function (index, ele) {
            str += `<option value="${ele._id}">${ele.cn_name}</option>`
          })
          $("#mysel").html(str)
        }
      })
    }

    showOne()
    // 添加二级分类
    $("#twoBtn").click(function () {
      $.ajax({
        url: "http://127.0.0.1:3000/cate/addTwoCate",
        type: "post",
        data: {
          oneId: $("#mysel").val(), //一级分类id
          two_name: $("#twoText").val() //二级分类名称
        },
        success: function (res) {
          console.log(res)
          alert(res.success)
          $("#twoText").val("")
        }
      })
    })
  </script>
</body>

</html>